#ifndef __INCcnotifyh
#define __INCcnotifyh

#include <queue>
#include <thread>
#include <condition_variable>
#include <vector>
#include <objbase.h>
#include <framework.h>

class Cnotify : public objbase
{
private:
    struct AsyncArgc
    {
        AsyncArgc(int sendId, int recvId, int cmdId, ArgList &reqArgs)
            : reqMsg(reqArgs), recvId(recvId), sendId(sendId), cmdId(cmdId)
        {
        }
        ArgList reqMsg;
        int     recvId;
        int     sendId;
        int     cmdId;
    };
    DISABLE_COPY_CLASS(Cnotify);

public:
    DECLARE_CTOR_DTOR(Cnotify);
    OBJBASE_EXTERN_MEMBER_IMPL();
    STATUS subRegReceiver(int recvId);
    STATUS subUnRegReceiver(int recvId);
    STATUS notify(int sendId, int recvId, int cmdId, ArgList &reqArgs, TextTable &outMsg, bool async = false);
    STATUS broadcast(int sendId, int cmdId, int recvfd = -1, bool async = false);
    STATUS asyncProc();

private:
    PROCESS_DESC(sysShutdown);
    STATUS asyncNotify(int sendId, int recvId, int cmdId, ArgList &reqArgs);

private:
    std::vector<int> m_observerList;
    std::queue<std::shared_ptr<AsyncArgc>> tasks;
    std::mutex m_taskLock;
    std::condition_variable cv_task;
    std::atomic<bool> stoped;
};

STATUS registerMessageListener(int recvId);
#endif // NOTIFIER_H
